<?php

namespace Acme\Likipe\BlogBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\Likipe\BlogBundle\Repository;

use Symfony\Component\HttpFoundation\RedirectResponse;

// Form
use Acme\Likipe\BlogBundle\Form\CommentType;

// Entity
use Acme\Likipe\BlogBundle\Entity\Post;
use Acme\Likipe\BlogBundle\Entity\Comment;


class CommentController extends Controller
{
    
    public function indexAction( $iId )
    {
    	$this->get( 'session' )
    		->setFlash( 'menu', '2' );
    	
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$cComment = $oEm->getRepository( 'AcmeLikipeBlogBundle:Post' )->find($iId);
    	
    	if ($cComment == null){
    		 throw $this
    		 	->createNotFoundException( 'No found Post by id ' . $iId . '!' );
    	}
    	
        return $this->render('AcmeLikipeBlogBundle:Comment:index.html.twig', 
        		array('post' => $cComment));
    }
    
    public function addAction( $iId, Request $oRequest ){
    	$this->get('session')
    		->setFlash('menu', '2');
    	
    	$em = $this->getDoctrine()->getEntityManager();
    	 
    	$oPost = $em->getRepository('AcmeLikipeBlogBundle:Post')->find($iId);
    	 
    	if ($oPost == null){
    		throw $this->createNotFoundException( 'No found Post by id '
    				. $iId . '!' );
    	}
    	
    	$oForm = $this->createForm( new CommentType(), new Comment() );
    	
    	if ($oRequest->getMethod() == 'POST'){
    		$oForm->bindRequest( $oRequest );
    		
    		if ( $oForm->isValid() ){
    			$cComment = new Comment();
    			$cComment = $oForm->getData();
    			$cComment->setCreated( new \DateTime( 'now' ));
    			$cComment->setPost($oPost);
    			
    			$em->persist( $cComment );
    			$em->flush();
    			
    			$this->get( 'session' )
    				->setFlash( 'notice', 'Add new Comment success!' );
    			
    			return new RedirectResponse( $this
    				->generateUrl( 'AcmeLikipeBlogBundle_comment', array(
    					'iId'	=> $iId
    				)));
    		}
    	}
    	
    	return $this->render( 'AcmeLikipeBlogBundle:Comment:add.html.twig',
    			array( 'form' => $oForm->createView(),
    					'post' => $oPost ));
    }
    
    public function deleteAction( $iId ){
    	$this->get( 'session' )
    		->setFlash( 'menu', '2' );
    		
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$cComment = $oEm->getRepository( 'AcmeLikipeBlogBundle:Comment' )
    		->find( $iId );
    	 
    	if ($cComment == null){
    		throw $this
    			->createNotFoundException( 'No found Comment by id ' . 
    					$iId . '!' );
    	
    	}else{  
    		$oPost = $cComment->getPost();
    		
	    	$oEm->remove( $cComment );
	    	$oEm->flush();
	    	 
	    	$this->get( 'session' )
	    		->setFlash( 'notice', 'Delete Comment with id ' . 
	    				$iId . ' success!' );
    	}
    	 
    	return new RedirectResponse( $this
    			->generateUrl( 'AcmeLikipeBlogBundle_comment', array(
    					'iId'	=> $oPost->getId()
    				)));
    }
    
    public function editAction( $iId, Request $oRequest ){
    	$this->get( 'session' )->setFlash( 'menu', '2' );
    	
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$cComment = $oEm->getRepository( 'AcmeLikipeBlogBundle:Comment' )
    		->find( $iId );
    	
    	if ($cComment == null){
    		throw $this
    			->createNotFoundException( 'No found Comment by id ' . 
    					$iId . '!' );
    	}
    	
    	$oPost = $cComment->getPost();
    	
    	$oForm = $this->createForm( new CommentType(), $cComment );
    	
    	if ( $oRequest->getMethod() == 'POST' ){
    		$oForm->bindRequest( $oRequest );
    		
    		$cComment = $oForm;
    		
    		$oEm->flush();
    		
    		$this->get( 'session' )
    		->setFlash( 'notice', 'Edit Comment with id ' . $iId . ' success!' );
    		
    		return new RedirectResponse($this
    				->generateUrl( 'AcmeLikipeBlogBundle_comment', array(
    						'iId'	=> $oPost->getId()
    				)));
    	}
    	
    	return $this->render( 'AcmeLikipeBlogBundle:Comment:edit.html.twig',
    			array( 'form' => $oForm->createView(),
    					'comment' => $cComment ));
    }
}
